/**
 * 
 */
package com.chen.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.apache.struts2.ServletActionContext;

import com.chen.bean.Users;
import com.chen.service.UserService;

/**
 * @author phn
 * 
 */
public class FileUploadAction {
	private Users user;
	private UserService userService;
	private File fileUpload;
	private String fileUploadFileName;
	private String fileUploadContentType;
	private String savePath;

	DateFormat df = new SimpleDateFormat("yyyyMMdd-hh-mm-ss-");
	String questionTime = df.format(new Date());


	HttpServletRequest request = ServletActionContext.getRequest();


	private  String secretId = "AKIDEH5btxoWu3waz8Bk5D6mDUVKs9B0Cz6x";
	private String secretKey = "kCONTUEk24qMegX9KGwReZIn47zi8JGz";
	COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
	Region region = new Region("ap-shanghai");
	ClientConfig clientConfig = new ClientConfig(region);
	COSClient cosClient = new COSClient(cred, clientConfig);





	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}

	public String getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}

	public String getFileUploadContentType() {
		return fileUploadContentType;
	}

	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String userPicUpload() throws Exception {
		//设置bucketName
		String bucketName = "kaiyuan-1259703767";
		//获取上传文件时间
		File localFile = new File(String.valueOf(getFileUpload()));
		//上传到指定目录 uploadPic
		String toFile="uploadPic";
        String key = toFile +"/"+ questionTime + getFileUploadFileName();
		FileInputStream fileInputStream = new FileInputStream(localFile);
		ObjectMetadata objectMetadata = new ObjectMetadata();
		// 设置输入流长度为文本长度
		objectMetadata.setContentLength(fileInputStream.available());
		// 设置 Content type, 默认是 application/octet-stream
		objectMetadata.setContentType("application/octet-stream");
		//上传
		try {
			PutObjectResult putObjectResult = cosClient.putObject(bucketName, key, fileInputStream, objectMetadata);
			String etag = putObjectResult.getETag();
			HttpSession session = ServletActionContext.getRequest().getSession();
			String p="https://kaiyuan-1259703767.cos.ap-shanghai.myqcloud.com/"+key;
			user = (Users) session.getAttribute("tu");
			user.setPicture(p);
			this.userService.update(user);
		}catch (Exception e){
			e.printStackTrace();
			return "fileupload_no";
		}

		//关闭输入流
		cosClient.shutdown();
		return "fileupload_ok";
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
